package com.lemner.motion.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lemner.motion.R;
import com.lemner.motion.base.BaseActivity;
import com.lemner.motion.base.BaseListener;
import com.lemner.motion.base.BaseModel;
import com.lemner.motion.bean.LoginBean;
import com.lemner.motion.fragment.HistFragment;
import com.lemner.motion.fragment.IndexFragment;
import com.lemner.motion.fragment.UserFragment;
import com.lemner.motion.util.FullScreen;


public class StartRunActivity extends BaseActivity implements HistFragment.OnFragmentInteractionListener,
        IndexFragment.OnFragmentInteractionListener,UserFragment.OnFragmentInteractionListener{

private  LinearLayout ll_hist,ll_index,ll_user;
private  Fragment fg_hist,fg_index,fg_user;
private  ImageView iv_hist,iv_index,iv_user;
private  FragmentManager fm;
FragmentTransaction ft;
private String  studentname,studentnum,studentpic,studentid,studentsex,schoolname;
private int totalMileage,totalTime;
    private long exitTime = 0;


    @Override
    public int getLayoutFile() {
        return R.layout.activity_start;
    }

    @Override
    public void initView() {
        FullScreen.fullScreen(this,0x00131313);
        getUser(LoginActivity.studentid,LoginActivity.session);
        ll_hist=(LinearLayout)findViewById(R.id.ll_hist);
        ll_index=(LinearLayout)findViewById(R.id.ll_index);
        ll_user=(LinearLayout)findViewById(R.id.ll_user);
        iv_hist=(ImageView)findViewById(R.id.iv_hist);
        iv_index=(ImageView)findViewById(R.id.iv_index);
        iv_user=(ImageView)findViewById(R.id.iv_user);
    }

    @Override
    public void initEvent() {
        ll_hist.setOnClickListener(this);
        ll_index.setOnClickListener(this);
        ll_user.setOnClickListener(this);
    }

    @Override
    public void initData() {
    }

    @Override
    public void onClick(View v) {
        ResetImage();
        switch (v.getId()) {
            case R.id.ll_hist:
                SetSelect(0);
                break;
            case R.id.ll_index:
                SetSelect(1);
                break;
            case R.id.ll_user:
                SetSelect(2);
                break;
            default:
                break;
        }
    }

    private void ResetImage(){
        iv_hist.setImageResource(R.drawable.hos0);
        iv_index.setImageResource(R.drawable.index0);
        iv_user.setImageResource(R.drawable.me0);
    }

    @SuppressLint("ResourceAsColor")
    private void SetSelect(int i){
        fm=getSupportFragmentManager();
        ft=fm.beginTransaction();
        HideFragement();
        switch (i){
            case 0:
                if(fg_hist==null){
                    fg_hist=new HistFragment();
                    ft.add(R.id.fl_content,fg_hist);
                }
                else{
                    ft.show(fg_hist);
                }
                iv_hist.setImageResource(R.drawable.hos1);
                break;
            case 1:
                if(fg_index==null){
                    fg_index=new IndexFragment();
                    Bundle bundle=new Bundle();
                    bundle.putString("school",schoolname);
                    bundle.putString("studentname",studentname);
                    bundle.putInt("totalMileage",totalMileage);
                    bundle.putInt("totalTime",totalTime);
                    fg_index.setArguments(bundle);
                    ft.add(R.id.fl_content,fg_index);
                }
                else{
                    ft.show(fg_index);
                }
                iv_index.setImageResource(R.drawable.index1);
                break;
            case 2:
                if(fg_user==null){
                    fg_user=new UserFragment();
                    Bundle bundle=new Bundle();
                    bundle.putString("studentname",studentname);
                    bundle.putString("studentnum",studentnum);
                    bundle.putString("studentpic",studentpic);
                    bundle.putString("studentid",studentid);
                    bundle.putString("studentsex",studentsex);
                    fg_user.setArguments(bundle);
                    ft.add(R.id.fl_content,fg_user);
                }
                else{
                    ft.show(fg_user);
                }
                iv_user.setImageResource(R.drawable.me1);
                break;

            default:
                break;
        }
        ft.commit();
    }
    public void HideFragement(){
        if(fg_hist!=null){
            ft.hide(fg_hist);
        }
        if(fg_index!=null){
            ft.hide(fg_index);
        }
        if(fg_user!=null){
            ft.hide(fg_user);
        }
        }

    BaseListener<LoginBean> listener=new BaseListener<LoginBean>() {
        @Override
        public void onResponse(LoginBean loginBean) {
            if(loginBean.getReturnCode().equals("0001")){
                 studentname=loginBean.getData().get(0).getStudentName();
                 studentnum=loginBean.getData().get(0).getStudentNum();
                 studentpic=loginBean.getData().get(0).getStudentPic();
                 studentid=loginBean.getData().get(0).getStudentId();
                 studentsex=loginBean.getData().get(0).getStudentSex();
                 totalMileage=loginBean.getData().get(0).getTotalMileage();
                 totalTime=loginBean.getData().get(0).getTotalTime();
                 schoolname=loginBean.getData().get(0).getSchoolName();
                 //个人资料返回标记
                if(getIntent().getBooleanExtra("flag",false))
                    SetSelect(2);
                //详情返回标记
//                else  if(getIntent().getBooleanExtra("list",false))
//                    SetSelect(0);
                //提交记录成功默认加载1
                else
                    SetSelect(1);
            }
            else{
                showToast("网络请求超时");

            }

        }

        @Override
        public void onFail(String msg) {
                //showToast(msg);
            SetSelect(1);
        }
    };


    public void getUser(String studentid,String session){
        BaseModel<LoginBean> model=new BaseModel<>();
        retrofit2.Call<LoginBean>call=  model.service.getUser(studentid,session);
        model.callEnqueue(call,listener);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                showToast("再按一次退出应用");
                exitTime = System.currentTimeMillis();
            } else {
                Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.addCategory(Intent.CATEGORY_HOME);
                startActivity(intent);
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}


